package com.cpi.cornpp.user.services;

import KC.ModSegApp.UsuarioApp;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.languaje.services.LanguageService;
import com.cpi.cornpp.languaje.view.LanguageView;
import com.cpi.cornpp.user.converter.UserConverter;
import com.cpi.cornpp.user.dao.UserDAO;
import com.cpi.cornpp.user.dto.UserDTO;
import com.cpi.cornpp.user.exception.UserException;
import com.cpi.cornpp.user.view.UserView;

/**
 * Handler the UserServices
 * 
 * @author Edgar Joao
 *
 */
public class UserServices {
	/**
	 * 
	 * @param userView
	 * @return UserView 
	 * @throws UserException 
	 */
	public UsuarioApp loginUser(UserView userView) throws UserException {
		UserDAO userDAO = null;
		UserConverter userConverter = null;
		UserDTO userDTO = null;
		UsuarioApp usuario = null;
		LanguageService languageService = null;
		LanguageView languageView = null;
		try {
			languageView = new LanguageView();
			languageService = new LanguageService();
			userDAO = new UserDAO();
			userConverter = new UserConverter();
			// La vista la convierto en un DTO
			userDTO = userConverter.convertUserViewToDto(userView);
			//Obtengo el usuario 
			usuario = userDAO.loginUser(userDTO);
			languageView = userConverter.convertUserToLanguageView(userView);
			languageService.addUserLanguage(languageView);
		} catch (UserException userException) {
			throw userException;
		} catch (Exception e){
			UserException userException = null;
			e.printStackTrace(System.out);
			userException = new UserException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw userException;
		}
		return usuario;
	}
	
}
